﻿namespace NEvoWeb.Modules.NB_Store
{
    using DotNetNuke.Common.Utilities;
    using DotNetNuke.Entities.Modules;
    using DotNetNuke.Services.Exceptions;
    using DotNetNuke.Services.Localization;
    using Microsoft.VisualBasic.CompilerServices;
    using System;
    using System.Collections;
    using System.Reflection;
    using System.Runtime.CompilerServices;
    using System.Web.UI;
    using System.Web.UI.WebControls;

    public class TaxRates : BaseAdminModule
    {
        [AccessedThroughProperty("cmdAddCategory")]
        private LinkButton _cmdAddCategory;
        [AccessedThroughProperty("cmdSearch")]
        private LinkButton _cmdSearch;
        [AccessedThroughProperty("cmdUpdate")]
        private LinkButton _cmdUpdate;
        [AccessedThroughProperty("cvShipTax")]
        private CompareValidator _cvShipTax;
        [AccessedThroughProperty("cvTaxDefault")]
        private CompareValidator _cvTaxDefault;
        [AccessedThroughProperty("ddlCategory")]
        private DropDownList _ddlCategory;
        [AccessedThroughProperty("grdRange")]
        private DataGrid _grdRange;
        [AccessedThroughProperty("lblShipTaxID")]
        private Label _lblShipTaxID;
        [AccessedThroughProperty("lblTDefaultID")]
        private Label _lblTDefaultID;
        [AccessedThroughProperty("lblTOptionsID")]
        private Label _lblTOptionsID;
        [AccessedThroughProperty("plShipTax")]
        private UserControl _plShipTax;
        [AccessedThroughProperty("plTDefault")]
        private UserControl _plTDefault;
        [AccessedThroughProperty("pnlCategory")]
        private Panel _pnlCategory;
        [AccessedThroughProperty("pnlEdit")]
        private Panel _pnlEdit;
        [AccessedThroughProperty("pnlProduct")]
        private Panel _pnlProduct;
        [AccessedThroughProperty("pnlRange")]
        private Panel _pnlRange;
        [AccessedThroughProperty("pnlSetup")]
        private Panel _pnlSetup;
        [AccessedThroughProperty("rblTaxOptions")]
        private RadioButtonList _rblTaxOptions;
        private string _TaxType;
        [AccessedThroughProperty("txtSearch")]
        private TextBox _txtSearch;
        [AccessedThroughProperty("txtShipTax")]
        private TextBox _txtShipTax;
        [AccessedThroughProperty("txtTDefault")]
        private TextBox _txtTDefault;

        public TaxRates()
        {
            base.Load += new EventHandler(this.Page_Load);
        }

        private void AddNewRecord(int ObjectID, string Description)
        {
            TaxController controller = new TaxController();
            NB_Store_TaxRatesInfo objInfo = new NB_Store_TaxRatesInfo {
                ItemID = 0,
                ObjectID = ObjectID,
                TaxDesc = Description,
                Disable = false,
                TaxType = this._TaxType,
                TaxPercent = "0",
                PortalID = this.PortalId
            };
            controller.UpdateObjTaxRate(objInfo);
            if (objInfo != null)
            {
                SharedFunctions.UpdateLog(this.UserId, this.ControlName + " - " + MethodBase.GetCurrentMethod().Name + " - " + XmlUtils.Serialize(objInfo));
            }
        }

        private void cmdAddCategory_Click(object sender, EventArgs e)
        {
            this.AddNewRecord(Conversions.ToInteger(this.ddlCategory.SelectedValue), this.ddlCategory.SelectedItem.Text);
            this.Response.Redirect(this.Request.Url.ToString());
        }

        private void cmdSearch_Click(object sender, EventArgs e)
        {
            this.grdRange.CurrentPageIndex = 0;
            this.populateRangeDG();
        }

        private void cmdUpdate_Click(object sender, EventArgs e)
        {
            try
            {
                this.updateRangeDG();
            }
            catch (Exception exception1)
            {
                ProjectData.SetProjectError(exception1);
                Exception exc = exception1;
                DotNetNuke.Services.Exceptions.Exceptions.ProcessModuleLoadException((PortalModuleBase) this, exc);
                ProjectData.ClearProjectError();
            }
        }

        private void grdRange_DeleteCommand(object source, DataGridCommandEventArgs e)
        {
            TaxController controller = new TaxController();
            NB_Store_TaxRatesInfo taxRate = controller.GetTaxRate(this.PortalId, Conversions.ToInteger(e.Item.Cells[1].Text));
            controller.DeleteTaxRate(Conversions.ToInteger(e.Item.Cells[1].Text));
            if (taxRate != null)
            {
                SharedFunctions.UpdateLog(this.UserId, this.ControlName + " - " + MethodBase.GetCurrentMethod().Name + " - " + XmlUtils.Serialize(taxRate));
            }
            this.Response.Redirect(this.Request.Url.ToString());
        }

        private void grdRange_PageIndexChanged(object source, DataGridPageChangedEventArgs e)
        {
            this.grdRange.CurrentPageIndex = e.NewPageIndex;
            this.populateRangeDG();
        }

        private void Page_Load(object sender, EventArgs e)
        {
            try
            {
                if (this.Request.QueryString["spg"] != null)
                {
                    switch (this.Request.QueryString["spg"].ToUpper())
                    {
                        case "SET":
                            this._TaxType = this.Request.QueryString["spg"].ToUpper();
                            break;

                        case "CAT":
                            this._TaxType = this.Request.QueryString["spg"].ToUpper();
                            break;

                        case "PRD":
                            this._TaxType = this.Request.QueryString["spg"].ToUpper();
                            break;
                    }
                    if (!this.Page.IsPostBack)
                    {
                        string str2 = this._TaxType.ToUpper();
                        if (str2 == "SET")
                        {
                            this.grdRange.Visible = false;
                            this.pnlCategory.Visible = false;
                            this.pnlProduct.Visible = false;
                            this.pnlSetup.Visible = true;
                            this.populateTDefault();
                            this.populateTaxOptions();
                        }
                        else if (str2 == "CAT")
                        {
                            this.pnlCategory.Visible = true;
                            this.pnlProduct.Visible = false;
                            this.pnlSetup.Visible = false;
                            this.populateRangeDG();
                            SharedFunctions.populateCategoryList(this.PortalId, this.ddlCategory);
                        }
                        else if (str2 == "PRD")
                        {
                            this.pnlCategory.Visible = false;
                            this.pnlProduct.Visible = true;
                            this.pnlSetup.Visible = false;
                            this.populateRangeDG();
                        }
                        else
                        {
                            this.pnlEdit.Visible = false;
                        }
                    }
                }
            }
            catch (Exception exception1)
            {
                ProjectData.SetProjectError(exception1);
                Exception exc = exception1;
                DotNetNuke.Services.Exceptions.Exceptions.ProcessModuleLoadException((PortalModuleBase) this, exc);
                ProjectData.ClearProjectError();
            }
        }

        private void populateRangeDG()
        {
            ArrayList list = new TaxController().GetTaxRatesList(this.PortalId, this._TaxType, SharedFunctions.GetCurrentCulture(), this.txtSearch.Text);
            this.grdRange.DataSource = list;
            this.grdRange.DataBind();
        }

        private void populateTaxOptions()
        {
            TaxController controller = new TaxController();
            NB_Store_TaxRatesInfo current = new NB_Store_TaxRatesInfo();
            this.rblTaxOptions.Items[0].Text = DotNetNuke.Services.Localization.Localization.GetString("rblTaxOptions1", this.LocalResourceFile);
            this.rblTaxOptions.Items[1].Text = DotNetNuke.Services.Localization.Localization.GetString("rblTaxOptions2", this.LocalResourceFile);
            this.rblTaxOptions.Items[2].Text = DotNetNuke.Services.Localization.Localization.GetString("rblTaxOptions3", this.LocalResourceFile);
            ArrayList taxRatesList = controller.GetTaxRatesList(this.PortalId, "OTX");
            if (taxRatesList == null)
            {
                this.lblTOptionsID.Text = "0";
                this.rblTaxOptions.SelectedValue = "1";
            }
            else if (taxRatesList.Count == 0)
            {
                this.lblTOptionsID.Text = "0";
                this.rblTaxOptions.SelectedValue = "1";
            }
            else
            {
                IEnumerator enumerator=null;
                try
                {
                    enumerator = taxRatesList.GetEnumerator();
                    while (enumerator.MoveNext())
                    {
                        current = (NB_Store_TaxRatesInfo) enumerator.Current;
                        this.lblTOptionsID.Text = current.ItemID.ToString();
                        this.rblTaxOptions.SelectedValue = Conversions.ToInteger(current.TaxPercent).ToString();
                    }
                }
                finally
                {
                    if (enumerator is IDisposable)
                    {
                        (enumerator as IDisposable).Dispose();
                    }
                }
            }
        }

        private void populateTDefault()
        {
            TaxController controller = new TaxController();
            NB_Store_TaxRatesInfo current = new NB_Store_TaxRatesInfo();
            ArrayList taxRatesList = controller.GetTaxRatesList(this.PortalId, "DTX");
            if (taxRatesList == null)
            {
                this.txtTDefault.Text = "0";
                this.lblTDefaultID.Text = "0";
            }
            else if (taxRatesList.Count == 0)
            {
                this.txtTDefault.Text = "0";
                this.lblTDefaultID.Text = "0";
            }
            else
            {
                IEnumerator enumerator=null;
                try
                {
                    enumerator = taxRatesList.GetEnumerator();
                    while (enumerator.MoveNext())
                    {
                        current = (NB_Store_TaxRatesInfo) enumerator.Current;
                        this.txtTDefault.Text = current.TaxPercent;
                        this.lblTDefaultID.Text = current.ItemID.ToString();
                    }
                }
                finally
                {
                    if (enumerator is IDisposable)
                    {
                        (enumerator as IDisposable).Dispose();
                    }
                }
            }
            taxRatesList = controller.GetTaxRatesList(this.PortalId, "STX");
            if (taxRatesList == null)
            {
                this.txtShipTax.Text = "0";
                this.lblShipTaxID.Text = "0";
            }
            else if (taxRatesList.Count == 0)
            {
                this.txtShipTax.Text = "0";
                this.lblShipTaxID.Text = "0";
            }
            else
            {
                IEnumerator enumerator2=null;
                try
                {
                    enumerator2 = taxRatesList.GetEnumerator();
                    while (enumerator2.MoveNext())
                    {
                        current = (NB_Store_TaxRatesInfo) enumerator2.Current;
                        this.txtShipTax.Text = current.TaxPercent;
                        this.lblShipTaxID.Text = current.ItemID.ToString();
                    }
                }
                finally
                {
                    if (enumerator2 is IDisposable)
                    {
                        (enumerator2 as IDisposable).Dispose();
                    }
                }
            }
        }

        private void updateRangeDG()
        {
            try
            {
                if (this.Page.IsValid)
                {
                    NB_Store_TaxRatesInfo info;
                    TaxController controller = new TaxController();
                    if (this.grdRange.Visible)
                    {
                        IEnumerator enumerator=null;
                        try
                        {
                            enumerator = this.grdRange.Items.GetEnumerator();
                            while (enumerator.MoveNext())
                            {
                                DataGridItem current = (DataGridItem) enumerator.Current;
                                info = new NB_Store_TaxRatesInfo {
                                    PortalID = this.PortalId,
                                    ItemID = Conversions.ToInteger(current.Cells[1].Text),
                                    ObjectID = Conversions.ToInteger(current.Cells[2].Text),
                                    TaxType = current.Cells[3].Text
                                };
                                if (current.Cells[4].Visible)
                                {
                                    info.TaxDesc = current.Cells[4].Text;
                                }
                                else
                                {
                                    info.TaxDesc = "";
                                }
                                try
                                {
                                    info.TaxPercent = Conversions.ToDecimal(((TextBox) current.FindControl("txtTaxPercent")).Text).ToString();
                                }
                                catch (Exception exception1)
                                {
                                    ProjectData.SetProjectError(exception1);
                                    Exception exception = exception1;
                                    info.TaxPercent = "0";
                                    ProjectData.ClearProjectError();
                                }
                                info.Disable = ((CheckBox) current.FindControl("chkDisable")).Checked;
                                if (info.TaxType == "PRD")
                                {
                                    if (Conversions.ToDouble(info.TaxPercent) > 0.0)
                                    {
                                        controller.UpdateObjTaxRate(info);
                                    }
                                }
                                else
                                {
                                    controller.UpdateObjTaxRate(info);
                                }
                                if (info != null)
                                {
                                    SharedFunctions.UpdateLog(this.UserId, this.ControlName + " - " + MethodBase.GetCurrentMethod().Name + " - " + XmlUtils.Serialize(info));
                                }
                            }
                        }
                        finally
                        {
                            if (enumerator is IDisposable)
                            {
                                (enumerator as IDisposable).Dispose();
                            }
                        }
                    }
                    if (this.pnlSetup.Visible)
                    {
                        TaxCalcController controller2 = new TaxCalcController(this.PortalId);
                        info = controller2.getDefaultTaxValue(this.PortalId, "DTX");
                        if (info == null)
                        {
                            info = new NB_Store_TaxRatesInfo {
                                ItemID = Conversions.ToInteger(this.lblTDefaultID.Text)
                            };
                        }
                        info.ObjectID = -1;
                        try
                        {
                            info.TaxPercent = Conversions.ToDecimal(this.txtTDefault.Text).ToString();
                        }
                        catch (Exception exception5)
                        {
                            ProjectData.SetProjectError(exception5);
                            Exception exception2 = exception5;
                            info.TaxPercent = "0";
                            ProjectData.ClearProjectError();
                        }
                        info.TaxType = "DTX";
                        info.TaxDesc = "Tax Default";
                        info.Disable = false;
                        info.PortalID = this.PortalId;
                        controller.UpdateObjTaxRate(info);
                        if (info != null)
                        {
                            SharedFunctions.UpdateLog(this.UserId, this.ControlName + " - " + MethodBase.GetCurrentMethod().Name + " - " + XmlUtils.Serialize(info));
                        }
                        info = controller2.getDefaultTaxValue(this.PortalId, "STX");
                        if (info == null)
                        {
                            info = new NB_Store_TaxRatesInfo {
                                ItemID = Conversions.ToInteger(this.lblShipTaxID.Text)
                            };
                        }
                        info.ObjectID = -1;
                        try
                        {
                            info.TaxPercent = Conversions.ToDecimal(this.txtShipTax.Text).ToString();
                        }
                        catch (Exception exception6)
                        {
                            ProjectData.SetProjectError(exception6);
                            Exception exception3 = exception6;
                            info.TaxPercent = "0";
                            ProjectData.ClearProjectError();
                        }
                        info.TaxType = "STX";
                        info.TaxDesc = "Ship Tax";
                        info.Disable = false;
                        info.PortalID = this.PortalId;
                        controller.UpdateObjTaxRate(info);
                        if (info != null)
                        {
                            SharedFunctions.UpdateLog(this.UserId, this.ControlName + " - " + MethodBase.GetCurrentMethod().Name + " - " + XmlUtils.Serialize(info));
                        }
                        info = controller2.getDefaultTaxValue(this.PortalId, "OTX");
                        if (info == null)
                        {
                            info = new NB_Store_TaxRatesInfo {
                                ItemID = Conversions.ToInteger(this.lblTOptionsID.Text)
                            };
                        }
                        info.ObjectID = -1;
                        info.TaxPercent = this.rblTaxOptions.SelectedValue;
                        info.TaxType = "OTX";
                        info.TaxDesc = "Tax Option";
                        info.Disable = false;
                        info.PortalID = this.PortalId;
                        controller.UpdateObjTaxRate(info);
                        if (info != null)
                        {
                            SharedFunctions.UpdateLog(this.UserId, this.ControlName + " - " + MethodBase.GetCurrentMethod().Name + " - " + XmlUtils.Serialize(info));
                        }
                    }
                    this.Response.Redirect(this.Request.Url.ToString());
                }
            }
            catch (Exception exception7)
            {
                ProjectData.SetProjectError(exception7);
                Exception exc = exception7;
                DotNetNuke.Services.Exceptions.Exceptions.ProcessModuleLoadException((PortalModuleBase) this, exc);
                ProjectData.ClearProjectError();
            }
        }

        protected virtual LinkButton cmdAddCategory
        {
            get
            {
                return this._cmdAddCategory;
            }
            [MethodImpl(MethodImplOptions.Synchronized)]
            set
            {
                EventHandler handler = new EventHandler(this.cmdAddCategory_Click);
                if (this._cmdAddCategory != null)
                {
                    this._cmdAddCategory.Click -= handler;
                }
                this._cmdAddCategory = value;
                if (this._cmdAddCategory != null)
                {
                    this._cmdAddCategory.Click += handler;
                }
            }
        }

        protected virtual LinkButton cmdSearch
        {
            get
            {
                return this._cmdSearch;
            }
            [MethodImpl(MethodImplOptions.Synchronized)]
            set
            {
                EventHandler handler = new EventHandler(this.cmdSearch_Click);
                if (this._cmdSearch != null)
                {
                    this._cmdSearch.Click -= handler;
                }
                this._cmdSearch = value;
                if (this._cmdSearch != null)
                {
                    this._cmdSearch.Click += handler;
                }
            }
        }

        protected virtual LinkButton cmdUpdate
        {
            get
            {
                return this._cmdUpdate;
            }
            [MethodImpl(MethodImplOptions.Synchronized)]
            set
            {
                EventHandler handler = new EventHandler(this.cmdUpdate_Click);
                if (this._cmdUpdate != null)
                {
                    this._cmdUpdate.Click -= handler;
                }
                this._cmdUpdate = value;
                if (this._cmdUpdate != null)
                {
                    this._cmdUpdate.Click += handler;
                }
            }
        }

        protected virtual CompareValidator cvShipTax
        {
            get
            {
                return this._cvShipTax;
            }
            [MethodImpl(MethodImplOptions.Synchronized)]
            set
            {
                this._cvShipTax = value;
            }
        }

        protected virtual CompareValidator cvTaxDefault
        {
            get
            {
                return this._cvTaxDefault;
            }
            [MethodImpl(MethodImplOptions.Synchronized)]
            set
            {
                this._cvTaxDefault = value;
            }
        }

        protected virtual DropDownList ddlCategory
        {
            get
            {
                return this._ddlCategory;
            }
            [MethodImpl(MethodImplOptions.Synchronized)]
            set
            {
                this._ddlCategory = value;
            }
        }

        protected virtual DataGrid grdRange
        {
            get
            {
                return this._grdRange;
            }
            [MethodImpl(MethodImplOptions.Synchronized)]
            set
            {
                DataGridCommandEventHandler handler = new DataGridCommandEventHandler(this.grdRange_DeleteCommand);
                DataGridPageChangedEventHandler handler2 = new DataGridPageChangedEventHandler(this.grdRange_PageIndexChanged);
                if (this._grdRange != null)
                {
                    this._grdRange.DeleteCommand -= handler;
                    this._grdRange.PageIndexChanged -= handler2;
                }
                this._grdRange = value;
                if (this._grdRange != null)
                {
                    this._grdRange.DeleteCommand += handler;
                    this._grdRange.PageIndexChanged += handler2;
                }
            }
        }

        protected virtual Label lblShipTaxID
        {
            get
            {
                return this._lblShipTaxID;
            }
            [MethodImpl(MethodImplOptions.Synchronized)]
            set
            {
                this._lblShipTaxID = value;
            }
        }

        protected virtual Label lblTDefaultID
        {
            get
            {
                return this._lblTDefaultID;
            }
            [MethodImpl(MethodImplOptions.Synchronized)]
            set
            {
                this._lblTDefaultID = value;
            }
        }

        protected virtual Label lblTOptionsID
        {
            get
            {
                return this._lblTOptionsID;
            }
            [MethodImpl(MethodImplOptions.Synchronized)]
            set
            {
                this._lblTOptionsID = value;
            }
        }

        protected virtual UserControl plShipTax
        {
            get
            {
                return this._plShipTax;
            }
            [MethodImpl(MethodImplOptions.Synchronized)]
            set
            {
                this._plShipTax = value;
            }
        }

        protected virtual UserControl plTDefault
        {
            get
            {
                return this._plTDefault;
            }
            [MethodImpl(MethodImplOptions.Synchronized)]
            set
            {
                this._plTDefault = value;
            }
        }

        protected virtual Panel pnlCategory
        {
            get
            {
                return this._pnlCategory;
            }
            [MethodImpl(MethodImplOptions.Synchronized)]
            set
            {
                this._pnlCategory = value;
            }
        }

        protected virtual Panel pnlEdit
        {
            get
            {
                return this._pnlEdit;
            }
            [MethodImpl(MethodImplOptions.Synchronized)]
            set
            {
                this._pnlEdit = value;
            }
        }

        protected virtual Panel pnlProduct
        {
            get
            {
                return this._pnlProduct;
            }
            [MethodImpl(MethodImplOptions.Synchronized)]
            set
            {
                this._pnlProduct = value;
            }
        }

        protected virtual Panel pnlRange
        {
            get
            {
                return this._pnlRange;
            }
            [MethodImpl(MethodImplOptions.Synchronized)]
            set
            {
                this._pnlRange = value;
            }
        }

        protected virtual Panel pnlSetup
        {
            get
            {
                return this._pnlSetup;
            }
            [MethodImpl(MethodImplOptions.Synchronized)]
            set
            {
                this._pnlSetup = value;
            }
        }

        protected virtual RadioButtonList rblTaxOptions
        {
            get
            {
                return this._rblTaxOptions;
            }
            [MethodImpl(MethodImplOptions.Synchronized)]
            set
            {
                this._rblTaxOptions = value;
            }
        }

        protected virtual TextBox txtSearch
        {
            get
            {
                return this._txtSearch;
            }
            [MethodImpl(MethodImplOptions.Synchronized)]
            set
            {
                this._txtSearch = value;
            }
        }

        protected virtual TextBox txtShipTax
        {
            get
            {
                return this._txtShipTax;
            }
            [MethodImpl(MethodImplOptions.Synchronized)]
            set
            {
                this._txtShipTax = value;
            }
        }

        protected virtual TextBox txtTDefault
        {
            get
            {
                return this._txtTDefault;
            }
            [MethodImpl(MethodImplOptions.Synchronized)]
            set
            {
                this._txtTDefault = value;
            }
        }
    }
}

